import ruleOpt from '@/biz/option/rule.js'
import moment from 'moment'
let _this = null

function section() {
  let reg = /^[1-9]\d*$/
  let validator = (rule, value, callback) => {
    if (value >= 15) {
      return callback('考核延长天数不可大于15天')
    }
    callback()
  }
  return {
    validator,
    trigger: 'blur'
  }
}

function ruleDate() {
  let validator = (rule, value, callback) => {
    if (getDaysBetween(_this.info.ruleStartTime, _this.info.ruleEndTime) >= 15) {
      return callback('规则生效期不可大于15天')
    }
    callback()
  }
  return {
    validator,
    trigger: 'blur'
  }
}

function ruleOptions(date) {
  return date && date.valueOf() < Date.now() - 86400000;
}

function getDaysBetween(dateString1, dateString2) {
  return moment(dateString2).diff(moment(dateString1), 'days');
}

function options(vm) {
  _this = vm
  let validDate = [
  {
    title: '考核项',
    key: 'sopName',
    text: {}
  }, {
    title: '考核延长',
    key: 'delayDay',
    group: [{
      key: 'delayDay',
      col: 12,
      placeholder: '请输入考核延长',
      input: {
        type: 'number'
      }
    }, {
      placeholder: '天',
      col: 6,
      text: {}
    }],
    rules: [{
        type: 'number',
        required: true,
        message: '请输入考核延长',
        trigger: 'blur'
      },
      ruleOpt.positiveNumber(),
      ruleOpt.positiveInteger(),
      // section(),
    ]
  },
  {
    title: '规则生效期',
    placeholder: '请选择日期范围',
    key: 'ruleStartTime',
    date: {
      type: 'daterange',
      disabled: (date) => ruleOptions(date)
    },
    data: {
      from: (info) => {
        if (info.ruleStartTime && info.ruleEndTime) {
          return [info.ruleStartTime, info.ruleEndTime]
        }
        return null
      },
      to: (v) => {
        return {
          ruleStartTime: v[0],
          ruleEndTime: v[1],
        }
      }
    },
    rules: [{
        required: true,
        message: '请选择规则生效期',
        trigger: 'blur',
      },
      // ruleDate()
    ]
  }]
  return validDate
}

export default {
  options
}